import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.Socket;
/**
 * 
 * @version : 2015年11月26日
 * @author 赵永春(zyc@hasor.net)
 */
public class ServerThread extends Thread {
    private int    clientID = 0;
    private Socket socket   = null;
    public ServerThread(Socket socket, int clientID) {
        this.socket = socket;
        this.clientID = clientID;
    }
    public void run() {
        try {
            //
            //Request
            Reader reader = new InputStreamReader(socket.getInputStream());
            BufferedReader ioRead = new BufferedReader(reader);
            String readLine = ioRead.readLine();
            String requestMethod = readLine.split(" ")[0];
            String requestURI = readLine.split(" ")[1];
            //
            System.out.println("Request[" + requestMethod + "]:" + requestURI);
            while ((readLine = ioRead.readLine()) != null) {
                System.out.println("[Client " + clientID + "] - " + readLine);
                if ("".equals(readLine.trim())) {
                    break;
                }
            }
            //
            //Response
            String body = "<form action='/addUser.do'><input type='text' name='userName'/><input type='submit' value='递交'/></form>";
            //
            PrintWriter outWriter = new PrintWriter(socket.getOutputStream());
            outWriter.println("HTTP/1.1 200 OK");
            outWriter.println("Connection:close");
            outWriter.println("Content-Type:text/html; charset=utf-8");
            outWriter.println("Keep-Alive:timeout=20");
            outWriter.println("\n");
            outWriter.println(body);
            //
            outWriter.close();
            ioRead.close();
            socket.close();
        } catch (Exception e) {
            System.out.println("Error. " + e);
        }
    }
}